package de.velvettear.squirrel.adapter;

import java.util.Vector;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import de.velvettear.squirrel.R;
import de.velvettear.squirrel.kernel.Password;

public class PasswordGeneratorAdapter extends ArrayAdapter {
	
	private Context context;
	
	private Vector<Password> passwords;
				
	public PasswordGeneratorAdapter(Context context, int layout, int textViewResourceId, Vector<Password> passwords) {
		super(context, layout, textViewResourceId, passwords);
		this.context = context;
		this.passwords = passwords;
	}
	
	@Override
	public int getCount() {
		return passwords.size();
	}

	@Override
	public Password getItem(int position) {
		return passwords.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		PasswordGeneratorViewHolder viewHolder;
		Password pass = passwords.get(position);	
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.listview_passwordgenerator, null);
			
			viewHolder = new PasswordGeneratorViewHolder();
			viewHolder.tv_value = (TextView) convertView.findViewById(R.id.listview_passwordgenerator_value);	
			
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (PasswordGeneratorViewHolder) convertView.getTag();
		}	
			
		viewHolder.tv_value.setText(pass.m_sValue);
			
		return convertView;	
	}

	static class PasswordGeneratorViewHolder {
		View convertView;
		TextView tv_value;
	}
	
}
